package cn.hyy.FVOnline.mapper;

import cn.hyy.FVOnline.entity.Address;
import cn.hyy.FVOnline.entity.Province;
import cn.hyy.FVOnline.entity.City;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("addressMapper")
public interface AddressMapper {

    @Results(id = "provinceMap", value = {
            @Result(property = "id", column = "id", id = true),
            @Result(property = "province", column = "provincename")
    })
    @Select("select * from address_province")
    List<Province> findAllProvince();

    @Results(id = "cityMap", value = {
            @Result(property = "id", column = "id", id = true),
            @Result(property = "city", column = "cityname"),
            @Result(property = "pid", column = "pid")
    })
    @Select("select * from address_city where pid = #{pid}")
    List<City> findPCity(@Param("pid") Integer pid);

    @Results(id = "addressMap",value = {
            @Result(property = "id", column = "id", id = true),
            @Result(property = "receiveName",column = "receivename"),
            @Result(property = "telephone", column = "telephone"),
            @Result(property = "province", column = "provincename"),
            @Result(property = "city", column = "cityname"),
            @Result(property = "details", column = "details"),
            @Result(property = "uid", column = "uid")
    })
    @Select("select * from address where uid = #{userId}")
    List<Address> findMyAddress(@Param("userId")Integer userId);

    @Delete("delete from address where id = #{id}")
    void delAddress(@Param("id")Integer id);

    @Insert("insert into address (receivename,telephone,provincename,cityname,details,uid) values " +
            "(#{address.receiveName}, #{address.telephone}, #{address.province}, #{address.city}, " +
            "#{address.details}, #{address.uid})")
    void addAddress(@Param("address")Address address);

    @ResultMap("addressMap")
    @Select("select * from address where id = #{id}")
    Address findById(@Param("id")Integer id);

}
